'use strict';

module.exports = app => {
    return class CommentService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }

        /**
         * 评贴
         * @param id
         * @returns {Promise<void>}
         */
        async add({ forumPostId, content }) {
            let url = this.app.urls('sendComment');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    forumPostId,
                    content
                }
            });
			// this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 回评
         * @param forumPostId
         * @param content
         * @returns {Promise<void>}
         */
        async sendTopic({ commentPostId, repliesTopicObjectId, repliesRoleType, content, sign }) {
            let url = this.app.urls('sendTopic');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    commentPostId,
                    repliesTopicObjectId,
                    repliesRoleType,
                    content,
                    sign
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /**
         * 根据帖子编号获取帖子评论
         * @param id
         */
        async list({ id, pageNo, pageSize }) {
            let url = this.app.urls('getCommentByPostId');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    forumPostId: id,
                    pageNo,
                    pageSize
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 根据帖子评论获取回评数据
         * @param id
         * @returns {Promise<void>}
         */
        async getCommentByCommentId({ id, pageNo, pageSize }) {
            let url = this.app.urls('getCommentByCommentId');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    commentPostId: id,
                    pageNo,
                    pageSize
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        async detail({ id }) {
            let url = this.app.urls('commentPostDetail');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }







    }

};
